<?php

namespace App\Models;


class Config extends Base
{
    protected $table = 'config';
    const SITE_KEY = 'site';

    public static function getSiteVal($cur_date = null) {
        if (!$cur_date)
            $cur_date = date('Y-m-d');
        $cache_key = 'config_key_' . $cur_date;

        return \Cache::remember($cache_key, 3600*24, function () use($cur_date){
            $val = self::where('key', self::SITE_KEY)->value('value');
            $arr = json_decode($val, true);

            $load_basic = json_decode($arr['load_basic'], true);
            $start = $load_basic[0];
            $end = $load_basic[1];
            $arr['load_start_at'] = date('Y-m-d H:i:s', strtotime($cur_date . " " . $start['time']) + $start['type'] * 24 * 3600);
            $arr['load_end_at'] = date('Y-m-d H:i:s', strtotime($cur_date . " " . $end['time']) + $end['type'] * 24 * 3600);
            if ($end['type'] > 0) {
                $arr['load_yesterday_end_at'] = date('Y-m-d H:i:s', strtotime($cur_date . " " . $end['time']));
            } else {
                $arr['load_yesterday_end_at'] = date('Y-m-d H:i:s', strtotime('-10 day'));
            }
            unset($arr['load_basic']);


            $appoint_basic = json_decode($arr['appoint_basic'], true);
            foreach ($appoint_basic as &$item) {
                $range = $item['range'];
                $start = $range[0];
                $end = $range[1];
                $start_at = date('Y-m-d H:i:s', strtotime($cur_date . " " . $start['time']) + $start['type'] * 24 * 3600);
                $end_at = date('Y-m-d H:i:s', strtotime($cur_date . " " . $end['time']) + $end['type'] * 24 * 3600);
                $item['start_at'] = $start_at;
                $item['end_at'] = $end_at;
                unset($item['range']);
            }
            $arr['appoint_basic'] = $appoint_basic;

            return $arr;
        });
    }

    public static function getNotifyStartTime() {
        $cache_key = 'notify_start_time';
        return \Cache::rememberForever($cache_key, function () {
            $val = self::where('key', self::SITE_KEY)->value('value');
            $arr = json_decode($val, true);
            return $arr['notify_start_at'];
        });
    }

    public static function getSignInClockTime() {
        $cache_key = 'sign_in_clock';
        return \Cache::rememberForever($cache_key, function () {
            $val = self::where('key', self::SITE_KEY)->value('value');
            $arr = json_decode($val, true);
            return $arr['sign_in_clock'];
        });
    }

    public static function getTotal() {
        $arr = self::getSiteVal();
        return $arr['qiao_num'] + $arr['wait_num'];
    }

    public static function getTime() {
        $arr = self::getSiteVal();
        $start = $arr['start_time'] ?? '00:01';
        $end = $arr['end_time'] ?? '23:59';
        return compact('start', 'end');
    }


    public static function getLoadTime() {
        $arr = self::getSiteVal();
        return compact('start_at', 'end_at', 'yesterday_end_at');
    }

    public static function isShowLoad() {
        $arr = self::getSiteVal();

        $cur_time = time();
        if ($cur_time <= strtotime($arr['load_yesterday_end_at'])) {
            return ['is_show' => true, 'val' => date("Y-m-d",strtotime("-1 day"))];
        } else if($cur_time >= strtotime($arr['load_start_at']) && $cur_time <= strtotime($arr['load_end_at'])) {
            return ['is_show' => true, 'val' => date('Y-m-d')];
        }
        return ['is_show' => false, 'val' => '2099-01-01'];
    }


    public static function getAppointTime() {
        $arr = self::getSiteVal();
        $cur_time = time();
        $appoint = $arr['appoint_basic'];
        foreach ($appoint as $item) {
            if ($cur_time >= strtotime($item['start_at']) && $cur_time <= strtotime($item['end_at'])) {
                return ['is_show' => true, 'start_at' => $item['start_at'], 'val' => date('Y-m-d', strtotime($item['end_at'])), 'punish' => $arr['punish'], 'queue_num_show' => $item['queue_num_show']];
            }
        }

        $max_date = max(array_column($appoint, 'end_at'));
        //判断当前是否大于最后面日期，如果大于向后面计算一天
        $tomorrow = date("Y-m-d",strtotime("+1 day"));
        if (time() > strtotime($max_date)) {

            $arr = self::getSiteVal($tomorrow);
            $cur_time = time();
            $appoint = $arr['appoint_basic'];
            foreach ($appoint as $item) {
                if ($cur_time >= strtotime($item['start_at']) && $cur_time <= strtotime($item['end_at'])) {
                    return ['is_show' => true, 'start_at' => $item['start_at'], 'val' => date('Y-m-d', strtotime($item['end_at'])), 'punish' => $arr['punish'], 'queue_num_show' => $item['queue_num_show']];
                }
            }


        }


        return ['is_show' => false, 'start_at' => '2099-01-01', 'val' => '2099-01-01', 'punish' => $arr['punish'], 'queue_num_show' => 0];
    }

}
